package com.ald.crossex.action.user;

import com.ald.crossex.dto.BaseResponse;
import com.ald.crossex.dto.EditUserRequest;
import com.ald.crossex.dto.JqGridRequest;
import com.ald.crossex.dto.JqGridResponse;
import com.ald.crossex.entity.TCeCoreUser;
import com.ald.crossex.service.UserService;
import com.ald.crossex.util.ErrorCode;
import com.ald.crossex.util.Page;
import com.ald.crossex.utils.PageUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by lishijiong on 15/1/29.
 */
@Controller
@RequestMapping(value = "/admin/user")
public class UserAction {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/sysUserManage")
    public String sysUserManage() throws  Exception{
        return "admin/user/sysUserManage";
    }

    @RequestMapping(value = "/appUserManage")
    public String appUserManage() {
        return "admin/user/appUserManage";
    }

    /**
     * 系统用户列表
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/sysUserList", method = RequestMethod.POST)
    public JqGridResponse listSysUser(@RequestBody JqGridRequest request) {

        Page page = userService.listSysUser(request);

        return PageUtils.pageToJqGridResponse(page);

    }

    /**
     * 应用用户列表
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/appUserList", method = RequestMethod.POST)
    public JqGridResponse listAppUser(@RequestBody JqGridRequest request) {

        Page page = userService.listAppUser(request);

        return PageUtils.pageToJqGridResponse(page);

    }

    /**
     * 新增、修改、删除用户
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/editUser", method = RequestMethod.POST)
    public JqGridResponse editUser(EditUserRequest request) throws Exception {
        String oper = request.getOper();
        TCeCoreUser user = new TCeCoreUser();
        user.setType(request.getType());
        user.setUsername(request.getUsername());
        user.setNickname(request.getNickname());
        user.setEmail(request.getEmail());
        user.setPhone(request.getPhone());
        try{
            if (oper.equals(JqGridRequest.JQGRIDREQUEST_OPER_ADD)) {
                userService.addUser(user);
            } else if (oper.equals(JqGridRequest.JQGRIDREQUEST_OPER_EDIT)) {
                user.setUid(request.getUid());
                userService.updateUser(user);
            } else if (oper.equals(JqGridRequest.JQGRIDREQUEST_OPER_DEL)) {
                if(userService.delUser(request.getId())<=0){
                    return new JqGridResponse(JqGridResponse.JQGRID_STATUS_ERROR);
                }
            }
        }catch (Exception e){
            return new JqGridResponse(JqGridResponse.JQGRID_STATUS_ERROR);
        }

        return new JqGridResponse(JqGridResponse.JQGRID_STATUS_SUCCESS);
    }


}
